//
//  IRMenuView.m
//

#import "IRMenuView.h"

#import "IRController.h"
#import "IRSelectionView.h"

@implementation IRMenuView

//--------------------------------------------------------------------------------------------------
- (id) initWithFrame:(NSRect) frameRect
{
	if (self = [super initWithFrame: frameRect])
	{
		int lineHeight = [[IRController sharedController] lineHeight];
		maxVisibleItems = frameRect.size.height / lineHeight;
		middleSlot = ((maxVisibleItems + 1) / 2) - 1;
	}

	return self;
}

//--------------------------------------------------------------------------------------------------
- (void) dealloc
{
	[menuItems release];

	[super dealloc];
}

//--------------------------------------------------------------------------------------------------
- (int) middleSlot
{
	return middleSlot;
}

//--------------------------------------------------------------------------------------------------
- (void) setItems:(NSArray*)list
{
	[menuItems release];
	menuItems = list;
	[menuItems retain];

	selectedIndex = 0;
	[self setNeedsDisplay: YES];
}

//--------------------------------------------------------------------------------------------------
- (int) selectedIndex
{
	return selectedIndex;
}

//--------------------------------------------------------------------------------------------------
- (IRMenuItem*) selectedItem
{
	return [menuItems objectAtIndex: selectedIndex];
}

//--------------------------------------------------------------------------------------------------
- (void) selectNext
{
	[self setSelectedIndex: selectedIndex + 1];
}

//--------------------------------------------------------------------------------------------------
- (void) selectPrevious
{
	[self setSelectedIndex: selectedIndex - 1];
}

//--------------------------------------------------------------------------------------------------
- (void) setSelectedItem:(NSString*) item
{
	int idx;
	for (idx = 0; idx < [menuItems count]; ++idx)
	{
		IRMenuItem *menuItem = [menuItems objectAtIndex: idx];
		if (NSOrderedSame == [item compare: [menuItem name]])
		{
			[self setSelectedIndex: idx];
			return;
		}
	}
}

//--------------------------------------------------------------------------------------------------
- (void) setSelectedIndex:(int) newIndex
{
	if (newIndex < 0)
	{
		newIndex = 0;
	}
	else
	if (newIndex >= [menuItems count])
	{
		newIndex = [menuItems count] - 1;
	}

	if (newIndex == selectedIndex)
	{
		return;
	}

	selectedIndex = newIndex;
	[self setNeedsDisplay: YES];
}

//--------------------------------------------------------------------------------------------------
- (void) drawRect:(NSRect) rect
{
	NSSize size = [self bounds].size;
	NSDictionary *attrs = [[IRController sharedController] menuAttributes];
	int lineHeight = [[IRController sharedController] lineHeight];

	int startSlot = 0;
//	int endSlot = maxVisibleItems - 1;
	int startIndex = selectedIndex - middleSlot;
	if (startIndex < 0)
	{
		startSlot += -startIndex;
		startIndex = 0;
	}

	int endIndex = startIndex + (maxVisibleItems - startSlot) - 1;
	if (endIndex >= [menuItems count])
	{
		endIndex = [menuItems count] - 1;
	}

	int slot = startSlot;
	int idx;

//NSLog(@"startSlot %d; endSlot %d, endIndex: %d", startSlot, endSlot, endIndex);
	for (idx = startIndex; idx <= endIndex; ++idx, ++slot)
	{
//NSLog(@"slot %d; index %d", slot, idx);
		IRMenuItem *menuItem = [menuItems objectAtIndex: idx];
		int y = size.height - ((slot + 1) * lineHeight);
		NSString *text = [menuItem name];

		NSRect rc = NSMakeRect(0, y, size.width, lineHeight);
		if (YES == [menuItem hasChildren])
		{
			rc.size.width -= 200;
		}
		else
		{
			rc.size.width -= 50;
		}

//		float grays[] = { 0.3 };
//		NSRectFillListWithGrays(&rc, grays, 1);

		NSMutableDictionary *attrs2 = [NSMutableDictionary dictionaryWithDictionary: attrs];
		NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
		[style setLineBreakMode:NSLineBreakByTruncatingTail];
		[attrs2 setObject:style  forKey:NSParagraphStyleAttributeName];

		[text drawInRect: rc  withAttributes: attrs2];

		if (YES == [menuItem hasChildren])
		{
			[@">" drawAtPoint: NSMakePoint(size.width - 200, y)  withAttributes: attrs];
		}
	}
}

@end


//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
@implementation IRMenuItem

//--------------------------------------------------------------------------------------------------
- (id) initWithName:(NSString*) nm  hasChildren:(bool)hasKids  obj:(NSObject*)obj
{
	if (self = [super init])
	{
		name = nm;
		[name retain];
		hasChildren = hasKids;
		object = obj;
		[object retain];
	}

	return self;
}

//--------------------------------------------------------------------------------------------------
- (void) dealloc
{
	[name release];
	[object release];

	[super dealloc];
}

//--------------------------------------------------------------------------------------------------
- (NSString*) name
{
	return name;
}

//--------------------------------------------------------------------------------------------------
- (bool) hasChildren
{
	return hasChildren;
}

//--------------------------------------------------------------------------------------------------
- (NSObject*) object
{
	return object;
}

@end
